package com.example.nacos.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("properties")
public class ReadNacosProperties {

    @NacosValue(value="${me.user:test}", autoRefreshed = true)
    private String meUser;
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/q")
    public Object test(){
        return meUser;
    }

    @PostConstruct
    public void test2(){

    }

    public void addClick(){
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.execute(()->{
            AtomicInteger atomicInteger = new AtomicInteger();
            while (true){
                atomicInteger.getAndIncrement();

                //获取1-10000的随机数
                int random = (int) (Math.random() * 10000 + 1);
                try {
                    System.out.println("睡眠时间："+random);
                    Thread.sleep(random);
                    System.out.println("睡眠结束："+random);
                    restTemplate.getForEntity("https://juejin.cn/post/7426282721545699337?share_token=dea8a6d7-36da-467e-9073-c5de7e9a1205",
                        String.class,new HashMap<>());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("调用次数："+atomicInteger.get());
            }
        });

    }
}
